/**
 * @file   RegisterClientRequestPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-03-11
 * @brief  DataBus Register Client Request packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/RegisterClientRequestPacket.h>

bool DataBus::RegisterClientRequestPacket::create(const quint8 source,
                                                  const quint8 packetId,
                                                  const quint8 activityTimeout,
                                                  const quint8 pingResponseTimeout,
                                                  const quint8 pingRetryCount,
                                                  Packet *packet)
{
    // Check parameters
    if ((source == 0) ||
        (activityTimeout < 10) ||
        (pingResponseTimeout < 1) ||
        (pingResponseTimeout > 60) ||
        (pingResponseTimeout > activityTimeout) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Create Header
    packet->setSource(source);
    packet->setDestination(0u);
    packet->setPacketType(PacketType_RegisterClientRequest);
    packet->setPacketId(packetId);

    // Create Payload
    QByteArray payload;

    payload.append(static_cast<char>(activityTimeout));
    payload.append(static_cast<char>(pingResponseTimeout));
    payload.append(static_cast<char>(pingRetryCount));

    packet->setData(payload);

    // Success
    return true;
}

bool DataBus::RegisterClientRequestPacket::parse(const Packet &packet,
                                                 quint8 *activityTimeout,
                                                 quint8 *pingResponseTimeout,
                                                 quint8 *pingRetryCount)
{
    // Check parameters
    if ((packet.getSource() == 0) ||
        (packet.getDestination() != 0) ||
        (packet.getPacketType() != PacketType_RegisterClientRequest) ||
        (activityTimeout == 0) ||
        (pingResponseTimeout == 0) ||
        (pingRetryCount == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get Payload data
    QByteArray data = packet.getData();

    if (data.size() != 3)
    {
        // Error, payload is not the correct size
        return false;
    }

    // Get activity timeout
    *activityTimeout = static_cast<quint8>(data.at(0));

    // Get ping response timeout
    *pingResponseTimeout = static_cast<quint8>(data.at(1));

    // Get ping retry count
    *pingRetryCount = static_cast<quint8>(data.at(2));

    // Success
    return true;
}
